﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;

namespace KandaAlpha.UI.Mvc.Infrastructure.Mvc
{
    /// <summary>
    /// uses to ioc to create controllers
    /// </summary>
    public class IoCControllerFactory : DefaultControllerFactory
    {

        protected override IController GetControllerInstance(Type controllerType)
        {
            try
            {
                if (controllerType == null)
                    throw new ArgumentNullException("controllerType");

                if (!typeof(IController).IsAssignableFrom(controllerType))
                    throw new ArgumentException(string.Format(
                        "Type requested is not a controller: {0}",
                        controllerType.Name),
                        "controllerType");
                return ServiceLocator.Current.GetInstance(controllerType) as IController;
            }
            catch (Exception ex)
            {
                return base.GetControllerInstance(controllerType); 
            }

        }
    }
}